package com.huluobo.h3weekdemo

import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.flyco.tablayout.listener.OnTabSelectListener
import com.huluobo.h3weekdemo.base.BaseMVVMActivity
import com.huluobo.h3weekdemo.databinding.ActivityMainBinding
import com.huluobo.h3weekdemo.db.DatabaseUtil
import com.huluobo.h3weekdemo.main.MainViewModel
import com.huluobo.h3weekdemo.main.TabBean
import com.huluobo.h3weekdemo.main.TabFragment
import com.huluobo.h3weekdemo.net.Const
import com.huluobo.h3weekdemo.search.SearchActivity
import java.util.ArrayList

class MainActivity : BaseMVVMActivity<ActivityMainBinding>() {
    private lateinit var viewModel: MainViewModel
    private val tabs = mutableListOf<String>()
    private val fragments = mutableListOf<Fragment>()
    private val tabIds = mutableListOf<Int>()
    private var newsTypeId: Int = 1
    override fun initData() {
        viewModel = ViewModelProvider(this)[MainViewModel::class.java]
        viewModel.getMainTabs()
        viewModel.success.observe(this) { it ->
            tabs.clear()
            //把数据源中每个tab的名字存起来
            it.data.forEach {
                tabs.add(it.typename)
                tabIds.add(it.id)
                DatabaseUtil.getDb().tabDao().insertTab(TabBean(it.id, it.typename))
            }
            //根据数据源的大小来生成对应几个fragment
            tabs.forEach { _ ->
                fragments.add(TabFragment())
            }
            //vp和tabLayout联动
            dataBinding.mainTabs.setViewPager(dataBinding.mainVp2, tabs.toTypedArray(), this, ArrayList(fragments))
            dataBinding.mainVp2.offscreenPageLimit = 1

            //设置初始化的id
            viewModel.setTypeId(tabIds[0])
        }

        viewModel.failed.observe(this) {
            val queryTabs = DatabaseUtil.getDb().tabDao().queryTabs()
            queryTabs.forEach {
                tabIds.add(it.id)
                tabs.add(it.typename)
            }
            tabs.forEach { _ ->
                fragments.add(TabFragment())
            }
            dataBinding.mainTabs.setViewPager(dataBinding.mainVp2, tabs.toTypedArray(), this, ArrayList(fragments))
            //设置初始化的id
            viewModel.setTypeId(tabIds[0])
        }
    }

    override fun initView() {
        dataBinding.mainVp2.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                //当翻页时,把需要请求的id存入vm
                viewModel.setTypeId(tabIds[position])
                newsTypeId = tabIds[position]
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })

        dataBinding.mainTabs.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                //当点击tab时,把需要请求的id存入vm
                viewModel.setTypeId(tabIds[position])
                newsTypeId = tabIds[position]
                
            }

            override fun onTabReselect(position: Int) {
            }
        })

        dataBinding.mainSearch.setOnClickListener {
            val intent = Intent(this, SearchActivity::class.java)
            intent.putExtra(Const.PARAM_NEWS_TYPE_ID, newsTypeId)
            startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_main
}